package com.guigu.pd.base.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.base.common.CustomIdGenerator;
import com.guigu.pd.base.entity.TruckDriverEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.base.entity.TruckEntity;
import com.guigu.pd.base.service.TruckService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**
 * 车辆信息表
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:09:01
 */
@RestController
@RequestMapping("base/truck")
public class TruckController {
    @Autowired
    private TruckService truckService;

    @Autowired
    private CustomIdGenerator customIdGenerator;

    /**
     * 分页查询车辆信息
     * @return
     */
    @GetMapping("/queryTruckfy")
    public R queryTruckfy(@RequestParam Map<String, Object> params){
        PageVo<TruckEntity> list = truckService.queryTruckfy(params);
        return R.ok().put("truck",list);
    }

    /**
     * 保存
     */
    @PostMapping("/saveTruckfy")
    public R save(@RequestParam Map<String, Object> params){
        TruckEntity truck = JSON.parseObject(JSON.toJSONString(params), TruckEntity.class);
        truck.setId(customIdGenerator.nextId(truck)+"");

        boolean save = truckService.save(truck);

        if(save){
            return R.ok("添加成功");
        }
        return R.ok("添加失败");
    }


    /**
     * 删除
     */
    @DeleteMapping("/deleteTruckEntitybyid/{id}")
    public R delete(@PathVariable String id){
        boolean b = truckService.removeById(id);

        if(b){
            return R.ok("删除成功");
        }

        return R.ok("删除失败");
    }



    @GetMapping("/gettruckinfobyid/{id}")
    public R gettruckInfobyId(@PathVariable(name = "id") String id) {
        TruckEntity truck = truckService.gettruckInfobyId(id);


        return R.ok().put("truck",truck).put("license",truck.getTruckLicense());
    }

    @RequestMapping("/getbyId/{fid}")
    public String gettruckbyfid(@PathVariable("fid") String fid){
        QueryWrapper<TruckEntity> queryWrapper = new QueryWrapper<TruckEntity>();
        queryWrapper.eq("fleet_id",fid);
        TruckEntity one = truckService.getOne(queryWrapper);
        return one.getLicensePlate();
    }

    @RequestMapping("/getnamebyId/{id}")
    public String gettrucknamebyid(@PathVariable("id") String id){
        TruckEntity one = truckService.getById(id);
        if(one!=null) {
            return one.getLicensePlate();
        }else {
            return null;
        }
    }

    @GetMapping("/gettrucklist")
    public R getTruckList(){
        List<TruckEntity> list = truckService.list();
        return R.ok().put("truck",list);
    }


}
